 /*
 * @lc app=leetcode.cn id=94 lang=javascript
 *
 * [94] 二叉树的中序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
// 非递归实现
// 利用栈这种数据结构后进先出的特点，来保证遍历顺序
var inorderTraversal = function(root) {
  let current = root
  const stack = []
  const res = []
  while (current || stack.length) {
    while(current) {
      stack.push(current)
      current = current.left
    }
    current = stack.pop()
    res.push(current.val)
    current = current.right
  }
  return res
};
// @lc code=end

// 递归实现
// 思路: 
// 判断节点是否为空
// 如果不为空，递归左节点，将根节点push，然后再递归右节点
// var inorderTraversal = function(root, list = []) {
//   if (!root) return list
//   inorderTraversal(root.left, list)
//   list.push(root.val)
//   inorderTraversal(root.right, list)
// };

